﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Extensions;
using DAL;

public partial class Controls_ProductList : System.Web.UI.UserControl
{
    protected int Index;

    protected void Page_Load(object sender, EventArgs e)
    {
        BLL.MenuManager mManager = new BLL.MenuManager();
        XMLMenu.Data = mManager.BuildXMLMenu("setCategory").ToString();
        DisplayList.NavigateUrl = Page.GetQueryStringNavigateUrl("d", "l", Page.Request);
        DisplayGrid.NavigateUrl = Page.GetQueryStringNavigateUrl("d", "g", Page.Request);
        
    }

    #region PListHorizontal
    public string Title
    {
        get
        {
            return TitleLabel.Text;
        }
        set
        {
            TitleLabel.Text = value;
        }
    }
    public int ItemsInBox
    {
        set
        {
            //DataPager.PageSize = value;
        }
    }
   
    protected string FormatPrice(object value)
    {
        return Convert.ToDecimal(value).ToMoneyString();
    }
    #endregion
    protected void Save_Click(object sender, EventArgs e)
    {
        using (DAL.EShopDBDataContext db = new EShopDBDataContext())
        {
            int catId = CategoryIdField.Value.ToInt32();
            foreach (ListViewItem li in ProductList.Items)
            {
                Controls_ProductListItem pI = li.FindControl("pItem") as Controls_ProductListItem;
                if (pI.Selected)
                {
                    var product = db.Products.Single(c => c.ProductId == pI.ProductId);
                    product.CategoryId = catId;
                }
            }
            db.SubmitChanges();
        }
        Response.Redirect(this.Request.Url.PathAndQuery);
    }

    protected void Pager1_Prerender(object sender, EventArgs e)
    {
        DataPager c = (DataPager)sender;

        if (c.TotalRowCount > c.PageSize)
            c.Visible = true;
        else
            c.Visible = false;
    }

    protected string GetShortDescription(string description)
    {
        int dLimit = 220;
        if (description.Length > dLimit)
        {
            description = string.Format("{0}...", description.Substring(0, dLimit));
        }
        return description;
    }

    protected void ProductList_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName.ToUpper() == "EDITREDIRECT")
        {
            ListViewDataItem lvdi = e.Item as ListViewDataItem;
            int catId = Request.QueryString["id"].ToInt32();
            Response.Redirect(string.Format("ProductEdit.aspx?id={0}&index={1}&catId={2}", e.CommandArgument, lvdi.DataItemIndex + 1, catId));
        }
        if (e.CommandName.ToUpper() == "CREATEPRODUCT")
        {
            using (EShopDBDataContext db = new EShopDBDataContext())
            {
                int categoryId = Convert.ToInt32(Request.QueryString["id"]);
                DAL.Product p = new DAL.Product { Title = "Новый Товар", CategoryId = categoryId, DefaultImageId = 1 };
                db.Products.InsertOnSubmit(p);
                db.SubmitChanges();
                Response.Redirect(string.Format("ProductEdit.aspx?id={0}", p.ProductId));
            }
        }
    }
    #region Template implementation
    private ITemplate dataSourceTemplate = null;
    protected DataSourceControl ProductsListDS;
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate DataSourceTemplate
    {
        get
        {
            return dataSourceTemplate;
        }
        set
        {
            dataSourceTemplate = value;
        }
    }

    void Page_Init()
    {
        if (dataSourceTemplate != null)
        {
            dataSourceTemplate.InstantiateIn(this);
            ProductsListDS = Controls.OfType<DataSourceControl>().Single();
        }
        if (!string.IsNullOrEmpty(Request.QueryString["d"]))
        {
            if (Request.QueryString["d"].ToLower() == "l")
                PLViews.ActiveViewIndex = 0;
            if (Request.QueryString["d"] == "g")
                PLViews.ActiveViewIndex = 1;
        }
        if (ProductsListDS != null)
        {
            if (PLViews.ActiveViewIndex == 0)
            {
                ProductList.DataSourceID = ProductsListDS.ID;
            }
            else if (PLViews.ActiveViewIndex == 1)
            {
                ProductsListH.DataSourceID = ProductsListDS.ID;
            }
        }
    }
    #endregion

}
